package com.skyable.account.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skyable.account.entity.SysPermission;
import com.skyable.account.service.IPermissionService;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.common.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GaoYuan
 * @title: PermissionController
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1510:23
 */
@Api(value = "资源权限管理")
@RestController
@RequestMapping("permission")
public class PermissionController extends BaseController{

    @Autowired
    private IPermissionService permissionService;

    @ApiOperation(value = "新增权限信息")
    @PostMapping("/insert")
    public ResponseResult insertPermission(@RequestBody SysPermission sysPermission, HttpServletRequest request) {
        setInsertInfo(sysPermission,request);
        ServiceResult<SysPermission> serviceResult = permissionService.insertPermission(sysPermission);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    @ApiOperation(value = "更新权限信息")
    @PutMapping("/update")
    public ResponseResult updatePermission(@RequestBody SysPermission sysPermission, HttpServletRequest request) {
        setUpdateInfo(sysPermission,request);
        ServiceResult<SysPermission> serviceResult = permissionService.updatePermission(sysPermission);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 获取权限列表
     * @return
     */
    @ApiOperation(value = "获取权限列表")
    @GetMapping("/view")
    public ResponseResult permission(){
        ServiceResult<List<SysPermission>> serviceResult = permissionService.findPermissionList();
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    @ApiOperation(value = "根据ID获取权限信息")
    @GetMapping("/view/{id}")
    public ResponseResult permission(@PathVariable("id") Long permissionId){
        ServiceResult<SysPermission> serviceResult = permissionService.findPermissionById(permissionId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 分页查询权限列表
     * @param page
     * @param limit
     * @param sysPermission
     * @return
     */
    @ApiOperation(value = "分页查询权限列表")
    @PostMapping("/view/{page}/{limit}")
    public ResponseResult permission(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit, @RequestBody SysPermission sysPermission){
        ServiceResult<IPage<SysPermission>> serviceResult = permissionService.findPermissionListByPage(page,limit,sysPermission);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult().getRecords());
    }

    @ApiOperation(value = "删除权限")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deletePermission(@PathVariable("id") Long permissionId){
        ServiceResult<Integer> serviceResult = permissionService.deletePermissionById(permissionId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }
}
